package com.sczmgk.track.activity;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import com.sczmgk.track.MyApp;
import com.sczmgk.track.R;
import com.sczmgk.track.activity.trackactivity.TracingActivity;
import com.sczmgk.track.base.BaseActivity;
import com.sczmgk.track.constant.Constant;
import com.sczmgk.track.db.impl.UserDaoImpl;
import com.sczmgk.track.entity.User;
import com.sczmgk.track.httpUtils.MyHttp;
import com.sczmgk.track.test.MyCallback;
import com.sczmgk.track.utils.Tools;
import com.sczmgk.track.utils.UIUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/11.
 * 设备激活界面
 */
public class DeviceActiveActivity extends BaseActivity implements View.OnClickListener {

    private static final int ACTIVE_SUCCESS = 1;
    private Button btnGoHome;
    private Button btnActive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_active);

        init();
    }

    private void init() {
        setHeader();
        initView();
        setListener();
    }

    private void setHeader() {
        setTitle(R.string.left_device_activation);
    }

    private void initView() {

        //进入首页
        btnGoHome = (Button) findViewById(R.id.btn_active_goin_home);

        //设备激活
        btnActive = (Button) findViewById(R.id.btn_active);
    }

    private void setListener() {
        btnActive.setOnClickListener(this);
        btnGoHome.setOnClickListener(this);
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case ACTIVE_SUCCESS:
                //TODO 展示激活成功的图片 需更改
                Tools.showChangeToast(DeviceActiveActivity.this, R.drawable.active_success);
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        UIUtils.startActivity(DeviceActiveActivity.this, TracingActivity.class);
                    }
                }, 3000);
                break;
        }

    }

    @Override
    public boolean getIsNeedLogin() {
        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_active:
                //立即激活
                showLoadingDiaLog();
                Map<String, Object> dataMaps = new HashMap<>();
                dataMaps.put("userid", MyApp.loginUserUid);
                dataMaps.put("imei", MyApp.imei);
                MyHttp.requestDataT(DeviceActiveActivity.this, Constant.DEVICE_ACTIVE_URL, MyHttp.POST, null, dataMaps, null, false, null, new MyCallback() {
                    @Override
                    public void onSuccess(Object o) {
                        dismissLoadingDiaLog();
                    }

                    @Override
                    public void onUnknownError() {
                        dismissLoadingDiaLog();
                    }

                    @Override
                    public void onError(String msg) {
                        showErrorMsg(msg);
                        dismissLoadingDiaLog();
                    }

                    @Override
                    public void onSuccess() {
                        dismissLoadingDiaLog();

                        User user = UserDaoImpl.getLoginUser(DeviceActiveActivity.this);
                        user.imeistatus = 0;
                        UserDaoImpl.getIntance(DeviceActiveActivity.this).updata(user);

                        mHandler.sendEmptyMessage(ACTIVE_SUCCESS);
                    }
                });
                break;
            case R.id.btn_active_goin_home:
                //进入首页
                UIUtils.startActivity(DeviceActiveActivity.this, TracingActivity.class);
                break;
        }
    }
}
